cmake_minimum_required(VERSION 3.16)

project(CurveDrawer VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets Charts)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Charts)

set(PROJECT_SOURCES
        excelreader.cpp
        logreader.cpp
        mycharts.cpp
        main.cpp
        mainwindow.cpp
        mainwindow.h
        mainwindow.ui
)

# 设置第三方库的头文件路径
include_directories(${CMAKE_SOURCE_DIR}/extern_headers)


if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(${PROJECT_NAME}
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}

        mycharts.h mycharts.cpp
        logreader.h logreader.cpp
        excelreader.h excelreader.cpp
        general.h
        script.json
        scriptreader.h scriptreader.cpp
        extern_headers/xlsxabstractooxmlfile.h extern_headers/xlsxabstractooxmlfile_p.h extern_headers/xlsxabstractsheet.h extern_headers/xlsxabstractsheet_p.h extern_headers/xlsxcell.h extern_headers/xlsxcell_p.h extern_headers/xlsxcellformula.h extern_headers/xlsxcellformula_p.h extern_headers/xlsxcelllocation.h extern_headers/xlsxcellrange.h extern_headers/xlsxcellreference.h extern_headers/xlsxchart.h extern_headers/xlsxchart_p.h extern_headers/xlsxchartsheet.h extern_headers/xlsxchartsheet_p.h extern_headers/xlsxcolor_p.h extern_headers/xlsxconditionalformatting.h extern_headers/xlsxconditionalformatting_p.h extern_headers/xlsxcontenttypes_p.h extern_headers/xlsxdatavalidation.h extern_headers/xlsxdatavalidation_p.h extern_headers/xlsxdatetype.h extern_headers/xlsxdocpropsapp_p.h extern_headers/xlsxdocpropscore_p.h extern_headers/xlsxdocument.h extern_headers/xlsxdocument_p.h extern_headers/xlsxdrawing_p.h extern_headers/xlsxdrawinganchor_p.h extern_headers/xlsxformat.h extern_headers/xlsxformat_p.h extern_headers/xlsxglobal.h extern_headers/xlsxmediafile_p.h extern_headers/xlsxnumformatparser_p.h extern_headers/xlsxrelationships_p.h extern_headers/xlsxrichstring.h extern_headers/xlsxrichstring_p.h extern_headers/xlsxsharedstrings_p.h extern_headers/xlsxsimpleooxmlfile_p.h extern_headers/xlsxstyles_p.h extern_headers/xlsxtheme_p.h extern_headers/xlsxutility_p.h extern_headers/xlsxworkbook.h extern_headers/xlsxworkbook_p.h extern_headers/xlsxworksheet.h extern_headers/xlsxworksheet_p.h extern_headers/xlsxzipreader_p.h extern_headers/xlsxzipwriter_p.h
        resources.qrc
        app.rc
        # 添加 .rc 文件

    )
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET CurveDrawer APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation



target_sources(${PROJECT_NAME} PRIVATE ${${PROJECT_NAME}_RESOURCES})
else()
    if(ANDROID)
        add_library(${PROJECT_NAME} SHARED
            ${PROJECT_SOURCES}
        )
# Define properties for Android with Qt 5 after find_package() calls as:
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    else()
        add_executable(${PROJECT_NAME}
            ${PROJECT_SOURCES}
            excelreader.cpp excelreader.h   logreader.cpp logreader.h mycharts.cpp mycharts.h
            # 其他源文件...
            app.rc  # 添加 .rc 文件
            resources.qrc
        )
    endif()
endif()

message("message ${CMAKE_SOURCE_DIR}/app.rc")

# qt_add_resources(CurveDrawer_RESOURCES "resources"
#     PREFIX "/"
#     FILES
#         resources.qrc
# )

# # 设置图标资源
# if(WIN32)
#     set_target_properties(CurveDrawer PROPERTIES
#         WIN32_EXECUTABLE TRUE
#         RESOURCE app.rc
#     )
# elseif(APPLE)
#     set(MACOSX_BUNDLE_ICON_FILE icon.ico)
#     set_source_files_properties(icon.ico PROPERTIES
#         MACOSX_PACKAGE_LOCATION "Resources"
#     )
#     set_target_properties(CurveDrawer PROPERTIES
#         MACOSX_BUNDLE TRUE
#         MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/Info.plist
#     )
# endif()



target_link_libraries(${PROJECT_NAME} PRIVATE
Qt${QT_VERSION_MAJOR}::Widgets
Qt${QT_VERSION_MAJOR}::Charts  # 链接 Qt Charts 模块
)

# 设置第三方库文件路径
link_directories(${CMAKE_SOURCE_DIR}/libs)

# 链接第三方库
if(${QT_VERSION} VERSION_LESS 6.0.0)
  # 根据构建类型链接不同的第三方库
  if(CMAKE_BUILD_TYPE STREQUAL "Debug")
      target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/libs/QXlsx-5d.lib)
  elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
      target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/libs/QXlsx-5.lib)
  endif()
else()
  # 根据构建类型链接不同的第三方库
  if(CMAKE_BUILD_TYPE STREQUAL "Debug")
      target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/libs/QXlsx-6d.lib)  # 修复：使用.lib导入库而不是.dll文件
  elseif(CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "MinSizeRel")
      target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/libs/QXlsx-6.lib)
  endif()
endif()



# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
if(${QT_VERSION} VERSION_LESS 6.1.0)
  set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.${PROJECT_NAME})
endif()

# 设置二进制目标文件输出目录为当前编译目录下的bin目录
# 为不同的构建类型分别设置输出目录，确保所有配置都输出到统一的bin目录：
# - RUNTIME_OUTPUT_DIRECTORY: 默认输出目录
# - RUNTIME_OUTPUT_DIRECTORY_DEBUG: Debug配置输出目录
# - RUNTIME_OUTPUT_DIRECTORY_RELEASE: Release配置输出目录
# - RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL: MinSizeRel配置输出目录
# - RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO: RelWithDebInfo配置输出目录
set_target_properties(${PROJECT_NAME} PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
    RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/bin
    RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/bin
    RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${CMAKE_BINARY_DIR}/bin
    RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${CMAKE_BINARY_DIR}/bin
)

set_target_properties(${PROJECT_NAME} PROPERTIES
    ${BUNDLE_ID_OPTION}
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)





include(GNUInstallDirs)
install(TARGETS ${PROJECT_NAME}
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(${PROJECT_NAME})
endif()



if(WIN32)

    # 把需要的第三方库复制到可执行文件目录
# POST_BUILD表示在目标构建完成后执行此命令
# 使用cmake -E copy_if_different命令仅在文件不同时才复制
# 根据不同的构建配置(CONFIG)选择相应的QXlsx库文件：
# - Debug配置使用QXlsx-6d.dll (d表示debug版本)
# - Release配置使用QXlsx-6.dll
# - MinSizeRel配置也使用QXlsx-6.dll
# $<TARGET_FILE_DIR:${PROJECT_NAME}>表示目标可执行文件的目录
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        $<$<CONFIG:Debug>:${CMAKE_SOURCE_DIR}/libs/QXlsx-6d.dll>
        $<$<CONFIG:Release>:${CMAKE_SOURCE_DIR}/libs/QXlsx-6.dll>
        $<$<CONFIG:MinSizeRel>:${CMAKE_SOURCE_DIR}/libs/QXlsx-6.dll>
        $<TARGET_FILE_DIR:${PROJECT_NAME}>
)

    # 使用windeployqt工具部署Qt依赖库到输出目录
# 查找windeployqt可执行文件，通过QTDIR环境变量或路径提示查找
find_program(WINDEPLOYQT_EXECUTABLE windeployqt HINTS ${QTDIR} ENV QTDIR PATH_SUFFIXES bin)
if(WINDEPLOYQT_EXECUTABLE)
    # POST_BUILD表示在目标构建完成后执行此命令
    # 调用windeployqt工具自动复制Qt运行时依赖库到可执行文件目录
    # 参数说明：
    # --dir: 指定输出目录为可执行文件所在目录
    # --libdir: 指定Qt库文件输出目录
    # --plugindir: 指定Qt插件输出目录
    # --qmldir: 指定QML文件输出目录
    # --no-compiler-runtime: 不复制编译器运行时库
    # --no-translations: 不复制翻译文件
    # $<TARGET_FILE:${PROJECT_NAME}>: 目标可执行文件路径
    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${WINDEPLOYQT_EXECUTABLE} 
            --dir $<TARGET_FILE_DIR:${PROJECT_NAME}>
            --libdir $<TARGET_FILE_DIR:${PROJECT_NAME}>
            --plugindir $<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins
            --qmldir $<TARGET_FILE_DIR:${PROJECT_NAME}>/qml
            --no-compiler-runtime
            --no-translations
            $<TARGET_FILE:${PROJECT_NAME}>
        COMMENT "Running windeployqt on ${PROJECT_NAME}..."
    )
endif()

    # 把项目目录下script.json文件复制到可执行文件目录
    # POST_BUILD表示在目标构建完成后执行此命令
    # 使用cmake -E copy_if_different命令仅在文件不同时才复制
    # $<TARGET_FILE_DIR:${PROJECT_NAME}>表示目标可执行文件的目录
    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
            ${CMAKE_SOURCE_DIR}/script.json
            $<TARGET_FILE_DIR:${PROJECT_NAME}>
    )

endif()
